package com.artisan.provider.utils;//package com.gubanjie.video.utils;


import com.artisan.provider.domain.ZgUserJurisdiction;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * jwt工具类
 */
public class JWTUtils {

    public static final String SUBJECT = "ezblu";

    public static final long EXPIRE = 1000*60*60*24; //过期时间1周

    public static final String APPSECRET = "ezb666"; //秘钥

    /**
     * 生成jwt
     * @param user
     * @return
     */
    public static String geneJsonWebToken(ZgUserJurisdiction user){

        //校验
        if (user == null || user.getId() == null || user.getAdminUserName() == null ){
            return null;
        }

        String token = Jwts.builder().setSubject(SUBJECT)
                .claim("id",user.getId())
                .claim("name",user.getAdminUserName())
                //.claim("img",user.getHeaderImg())
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis()+EXPIRE))
                .signWith(SignatureAlgorithm.HS256,APPSECRET).compact();
        return token;
    }


    /**
     * jwt:token 解密
     * 校验token
     */
    public static Claims checkJWT(String token){
        try {
        final Claims claims = Jwts.parser().setSigningKey(APPSECRET).parseClaimsJws(token).getBody();
            return claims;

        } catch (Exception e) {
            e.printStackTrace();
        }
            return null;

    }

}
